<template>
  <div>
    <city-header></city-header>
    <city-search :cities="cities"></city-search>
    <city-list :hot="hotCities" :cities="cities" :letter="letter"></city-list>
    <city-alphabet :cities="cities" @change="handleLetter"></city-alphabet>
  </div>
</template>

<script>
import axios from "axios";
import CityHeader from "../components/city/Header.vue";
import CitySearch from "../components/city/Search.vue";
import CityList from "../components/city/List.vue";
import CityAlphabet from "../components/city/Alphabet.vue";
export default {
  name: "City",
  components: {
    CityHeader,
    CitySearch,
    CityList,
    CityAlphabet,
  },
  data() {
    return {
      cities: {},
      hotCities: [],
      letter: "",
    };
  },
  methods: {
    // 用于发送 axios 请求
    getCityInfo() {
      axios
        .get(
          "https://www.fastmock.site/mock/bf89f5eb4e2c2bbc6a707ef62de3f1d1/api/city.json"
        )
        .then(this.getCityInfoSucc);
    },
    // 请求成功执行的函数
    getCityInfoSucc(res) {
      // console.log(res, res.data);
      if (res?.data?.ret && res?.data?.data) {
        const { data } = res.data;
        // console.log(data);
        this.cities = data.cities;
        this.hotCities = data.hotCities;
      }
    },
    handleLetter(letter) {
      this.letter = letter;
    },
  },
  mounted() {
    this.getCityInfo();
  },
};
</script>

<style lang="scss">
</style>